package com.moore.service.Impl;

import com.moore.bean.Admin;
import com.moore.dto.ResponseData;
import com.moore.mapper.AdminMapper;
import com.moore.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static com.moore.utils.Logger.toLog;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ResponseData login(Admin admin) {
        List<Admin> admins = adminMapper.login(admin.getAccount(), admin.getPassword());
        if (admins.isEmpty()) {
            toLog("管理员账号或密码错误");
            return new ResponseData(400,"账号或密码错误",null,0);
        }
        if (admins.get(0).getStatus().equals("disabled")) {
            toLog("管理员账号被封禁");
            return new ResponseData(400,"账号被封禁",null,0);
        }
        else {
            toLog("管理员登录成功");
            return new ResponseData(200,"登录成功",admins.get(0),1);
        }
    }

    @Override
    public ResponseData updPwd(Admin admin) {
        int result = adminMapper.updatePassword(admin.getId(), admin.getPassword(), admin.getOldPassword());
        if (result == 1) {
            toLog("修改密码成功");
            return new ResponseData(200,"修改成功",null,1);
        } else {
            toLog("修改密码失败，旧密码不匹配");
            return new ResponseData(400,"旧密码不匹配，请重试",null,0);
        }
    }

    @Override
    public ResponseData getAllAdmin(Admin admin) {
        toLog("管理人员列表获取成功");
        List<Admin> admins = adminMapper.getAllAdmin(admin);
        return new ResponseData(200,"查询成功",admins,adminMapper.getAdminCount(admin));
    }

    @Override
    public ResponseData addAdmin(Admin admin) {
        int result = adminMapper.addAdmin(admin);
        if (result == 1) {
            toLog("添加管理员成功");
            return new ResponseData(200,"添加成功",null,1);
        } else {
            toLog("添加管理员失败");
            return new ResponseData(400,"添加失败",null,0);
        }
    }

    @Override
    public ResponseData updateAdmin(Admin admin) {
        int result = adminMapper.updateAdmin(admin);
        if (result == 1) {
            toLog("修改管理员信息成功");
            return new ResponseData(200,"修改成功",null,1);
        } else {
            toLog("修改管理员信息失败");
            return new ResponseData(400,"修改失败",null,0);
        }
    }

    @Override
    public ResponseData adminSetStatus(Admin admin) {
        int result = adminMapper.adminSetStatus(admin.getId(), admin.getStatus());
        String status = admin.getStatus().equals("enable")? "启用":"禁用";
        if (result == 1) {
            toLog("管理员状态修改成功");
            return new ResponseData(200, status + "成功",null,1);
        } else {
            toLog("管理员状态修改失败");
            return new ResponseData(400,status + "失败",null,0);
        }
    }

    @Override
    public ResponseData deleteAdmin(Admin admin) {
        int result = adminMapper.deleteAdmin(admin.getId());
        if (result == 1) {
            toLog("删除管理员成功");
            return new ResponseData(200,"删除成功",null,1);
        } else {
            toLog("删除管理员失败");
            return new ResponseData(400,"删除失败",null,0);
        }
    }

    @Override
    public ResponseData resetPassword(Admin admin) {
        int result = adminMapper.resetPassword(admin.getId());
        if (result == 1) {
            toLog("重置密码成功");
            return new ResponseData(200,"重置成功",null,1);
        } else {
            toLog("重置密码失败");
            return new ResponseData(400,"重置失败",null,0);
        }
    }
}
